package com.sbox.db;

import java.util.Calendar;

import com.sbox.model.NoteItem;
import com.sbox.util.SBoxLog;

import android.database.Cursor;

public class NoteDAO extends BaseDAO<NoteItem> {
	public static final String NOTE_SELECT_ALL = "SELECT * FROM notes ORDER BY id DESC";		
	public static final String NOTE_SELECT_ID_TITLE = "SELECT id, title FROM notes ORDER BY id DESC";
	
	public static final String NOTE_SELECT_BY_ID = "SELECT * FROM notes WHERE id=?";
	public static final String NOTE_SELECT_AFTER = "SELECT * from notes WHERE id>=?";
	
	public static final String NOTE_INSERT = "INSERT into notes(id,title,content) values(?,?,?)";
	public static final String NOTE_UPDATE = "UPDATE notes SET title=?, content=? WHERE id=?";
	public static final String NOTE_DELETE = "DELETE FROM notes where id=?";
	public static final String NOTE_DELETE_ALL = "DELETE FROM notes";
	
	@Override
	public NoteItem[] createRows(Cursor c)
	{
		NoteItem[] items = new NoteItem[c.getCount()];		
		
		return items;
	}

	@Override
	public NoteItem mapRow(Cursor c)
	{
		NoteItem note = new NoteItem();
		note.ID = getLong(c, "id");
		note.Title = getString(c, "title");
		note.Content = getString(c, "content");
		
		return note;			
	}
	
	public NoteItem loadNote(long id)
	{
		Cursor c = null;
		NoteItem note = null;
		
		c = mDatabase.rawQuery(NOTE_SELECT_BY_ID, new String[]{String.valueOf(id)} );
		if( c == null )
		{
			SBoxLog.error("loadNote, cursor==null");
			return null;
		}
		if( c.getCount() == 0 )
		{
			return null;
		}		
		
		if( !c.moveToFirst() )
		{
			SBoxLog.error("loadNote, moveToFirst()==null");
			return null;
		}				
		note = mapRow(c);
		
		c.close();
		
		return note;
	}
	
	public NoteItem[] loadNoteTitles()
	{		
		Cursor cursor = null;
		NoteItem[] notes = null;
		
		cursor = mDatabase.rawQuery(NoteDAO.NOTE_SELECT_ID_TITLE, null);		
		if( cursor == null )
		{
			return null;
		}		
		
		notes = mapRows(cursor);
		cursor.close();
		
		if( notes == null )
			return null;
			
		return notes;
	}
	
	public NoteItem[] loadNotesAfter(long time) {
		Cursor cursor = null;
		NoteItem[] notes = null;
		
		cursor = mDatabase.rawQuery(NoteDAO.NOTE_SELECT_AFTER, new String[]{String.valueOf(time)});		
		if( cursor == null )
		{
			return null;
		}		
		
		notes = mapRows(cursor);
		cursor.close();
		
		if( notes == null )
			return null;
			
		return notes;
	}
	
	public void  insertNote(NoteItem note)
	{
		Object[] params = new Object[3];			
		
		if( note.ID == 0 )
			note.ID = Calendar.getInstance().getTimeInMillis();
		
		SBoxLog.info("INSERT, ID=" + String.valueOf(note.ID) + " , Title=" + note.Title);
		params[0] = note.ID;
		params[1] = note.Title;
		params[2] = note.Content;
		
		mDatabase.execSQL(NoteDAO.NOTE_INSERT, params);
	}
	
	public void updateNote(NoteItem note)
	{
		Object[] params = new Object[3];
		
		SBoxLog.info("UPDATE, ID=" + String.valueOf(note.ID) + " , Title=" + note.Title);
		params[0] = note.Title;
		params[1] = note.Content;
		params[2] = note.ID;
		
		mDatabase.execSQL(NoteDAO.NOTE_UPDATE, params);
	}
	
	public void deleteNote(long id)
	{
		if( id != 0 )
		{			
			mDatabase.execSQL(NoteDAO.NOTE_DELETE, new String[]{String.valueOf(id)});
		}
	}
	
	public void deleteNote(NoteItem note)
	{
		if( note.ID != 0 )
		{			
			mDatabase.execSQL(NoteDAO.NOTE_DELETE, new String[]{String.valueOf(note.ID)});
		}
	}
	
	public void deleteAllNotes()
	{
		mDatabase.execSQL(NoteDAO.NOTE_DELETE_ALL);
	}
}
